package com.toaa.crazymath;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.games.Games;
import com.toaa.crazymath.Constant.GameDifficulty;
import com.toaa.crazymath.Constant.GameMode;
import com.vn.toaa.brainracer.R;


public class MenuFragment extends Fragment implements OnClickListener{
	
	public static final String TAG = MenuFragment.class.getSimpleName();
	private Button mBtPlay;
	private Button mBtDifficulty;
	private Button mBtLeader;
	private Button mBtRate;
	private Button mBtMode;
	private Button mBtShare;
	private Button mBtAchievement;
	private Dialog mDialog;
	private SharedPreferences mPreferences;
	private MyApplication mApplication;
	private Animation mAnimOutUp;
	private View mRootView;
	private Typeface mTypeface;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mApplication = (MyApplication) getActivity().getApplication();
		mPreferences = getActivity().getSharedPreferences(getActivity().getPackageName(), Context.MODE_PRIVATE);
		mTypeface = Typeface.createFromAsset(getActivity().getAssets(), "UVNThangVu.TTF");
		mAnimOutUp = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_out_up);
		mAnimOutUp.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				getActivity().getSupportFragmentManager().beginTransaction()
				.setTransition(R.anim.slide_out_up)
						.replace(R.id.framelayout_container, new PlayFragment()).commit();
			}
		});
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mRootView = inflater.inflate(R.layout.menu_fragment, null);
		mBtPlay = (Button) mRootView.findViewById(R.id.button_play);
		mBtDifficulty = (Button) mRootView.findViewById(R.id.button_difficulty);
		mBtLeader = (Button) mRootView.findViewById(R.id.button_leaderboard);
		mBtRate = (Button) mRootView.findViewById(R.id.button_rate);
		mBtMode = (Button) mRootView.findViewById(R.id.button_mode);
		mBtShare = (Button) mRootView.findViewById(R.id.button_share);
		mBtAchievement = (Button) mRootView.findViewById(R.id.button_achievement);
		
		mBtPlay.setOnClickListener(this);
		mBtDifficulty.setOnClickListener(this);
		mBtLeader.setOnClickListener(this);
		mBtRate.setOnClickListener(this);
		mBtMode.setOnClickListener(this);
		mBtShare.setOnClickListener(this);
		mBtAchievement.setOnClickListener(this);
		return mRootView;
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_play:
//			mRootView.startAnimation(mAnimOutUp);
			getActivity().getSupportFragmentManager().beginTransaction()
					.replace(R.id.framelayout_container, new PlayFragment(), Constant.FRAGMENT_TAG_PLAY).commit();
			break;
		case R.id.button_difficulty:
			displayDialogById(1);
			break;
		case R.id.button_mode:
			displayDialogById(2);
			break;
		case R.id.button_share:
			final Intent intent = new Intent(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_message));
			try {
				startActivity(Intent.createChooser(intent, "Select an action"));
			} catch (android.content.ActivityNotFoundException ex) {
				// (handle error)
			}
			break;
		case R.id.button_achievement:
			System.out.println();
			try {
				startActivityForResult(
						Games.Achievements.getAchievementsIntent(((MainActivity) getActivity()).getApiClient()), 1);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case R.id.button_easy:
			v.setBackgroundColor(getResources().getColor(R.color.blue));
			dismissDialog();
			mPreferences.edit().putInt(Constant.PREF_GAME_DIFFICULTY, GameDifficulty.EASY.ordinal()).commit();
			break;
		case R.id.button_normal:
			v.setBackgroundColor(getResources().getColor(R.color.blue));
			dismissDialog();
			mPreferences.edit().putInt(Constant.PREF_GAME_DIFFICULTY, GameDifficulty.NORMAL.ordinal()).commit();
			break;
		case R.id.button_hard:
			v.setBackgroundColor(getResources().getColor(R.color.blue));
			dismissDialog();
			mPreferences.edit().putInt(Constant.PREF_GAME_DIFFICULTY, GameDifficulty.HARD.ordinal()).commit();
			break;
		case R.id.button_mode_addition:
			v.setBackgroundColor(getResources().getColor(R.color.blue));
			dismissDialog();
			mPreferences.edit().putInt(Constant.PREF_GAME_MODE, GameMode.ADDITION.ordinal()).commit();
			break;
		case R.id.button_mode_subtraction:
			v.setBackgroundColor(getResources().getColor(R.color.blue));
			dismissDialog();
			mPreferences.edit().putInt(Constant.PREF_GAME_MODE, GameMode.SUBTRACTION.ordinal()).commit();
			break;
		case R.id.button_mode_multiplication:
			v.setBackgroundColor(getResources().getColor(R.color.blue));
			dismissDialog();
			mPreferences.edit().putInt(Constant.PREF_GAME_MODE, GameMode.MULTIPLICATION.ordinal()).commit();
			break;
		case R.id.button_mode_division:
			v.setBackgroundColor(getResources().getColor(R.color.blue));
			dismissDialog();
			mPreferences.edit().putInt(Constant.PREF_GAME_MODE, GameMode.DIVISION.ordinal()).commit();
			break;
		default:
			break;
		}
	}

	private void displayDialogById(int id) {
		if (id == 1) {
			mDialog = new Dialog(getActivity());
			mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
			final View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_select_difficulty, null);
			final Button btEasy = (Button) view.findViewById(R.id.button_easy);
			final Button btNormal = (Button) view.findViewById(R.id.button_normal);
			final Button btHard = (Button) view.findViewById(R.id.button_hard);
			final TextView tvTitle = (TextView) view.findViewById(R.id.textview_difficulty_title);
			btEasy.setOnClickListener(this);
			btNormal.setOnClickListener(this);
			btHard.setOnClickListener(this);
//			btEasy.setTypeface(Typeface.SERIF);
//			btNormal.setTypeface(Typeface.SERIF);
//			btHard.setTypeface(Typeface.SERIF);
			tvTitle.setTypeface(mTypeface);
			final int prefDifficulty = mPreferences.getInt(Constant.PREF_GAME_DIFFICULTY, GameDifficulty.NORMAL.ordinal());
			if (prefDifficulty == GameDifficulty.EASY.ordinal()) {
				btEasy.setBackgroundColor(getResources().getColor(R.color.blue));
			} else if (prefDifficulty == GameDifficulty.NORMAL.ordinal()) {
				btNormal.setBackgroundColor(getResources().getColor(R.color.blue));
			} else if (prefDifficulty == GameDifficulty.HARD.ordinal()) {
				btHard.setBackgroundColor(getResources().getColor(R.color.blue));
			}
			
			WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
			final int screenWidth = windowManager.getDefaultDisplay().getWidth();
			final int defaultWidth = (int) (screenWidth * 0.8);
//			int defaultHeight = (int) (mApplication.getDimensionDevice().y * 0.9);
			mDialog.setContentView(view);
			mDialog.getWindow().setLayout(defaultWidth, LayoutParams.WRAP_CONTENT);
		} else if (id == 2) {
			mDialog = new Dialog(getActivity());
			mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
			final View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_select_mode, null);
			final Button btAdd = (Button) view.findViewById(R.id.button_mode_addition);
			final Button btSubtract = (Button) view.findViewById(R.id.button_mode_subtraction);
			final Button btMulti = (Button) view.findViewById(R.id.button_mode_multiplication);
			final Button btDivision = (Button) view.findViewById(R.id.button_mode_division);
			final TextView tvTitle = (TextView) view.findViewById(R.id.textview_mode_title);
			btAdd.setOnClickListener(this);
			btSubtract.setOnClickListener(this);
			btMulti.setOnClickListener(this);
			btDivision.setOnClickListener(this);
			
//			btAdd.setTypeface(Typeface.SERIF);
//			btSubtract.setTypeface(Typeface.SERIF);
//			btMulti.setTypeface(Typeface.SERIF);
//			btDivision.setTypeface(Typeface.SERIF);
			tvTitle.setTypeface(mTypeface);
			final int prefMode = mPreferences.getInt(Constant.PREF_GAME_MODE, GameMode.ADDITION.ordinal());
			if (prefMode == GameMode.ADDITION.ordinal()) {
				btAdd.setBackgroundColor(getResources().getColor(R.color.blue));
			} else if (prefMode == GameMode.SUBTRACTION.ordinal()) {
				btSubtract.setBackgroundColor(getResources().getColor(R.color.blue));
			} else if (prefMode == GameMode.MULTIPLICATION.ordinal()) {
				btMulti.setBackgroundColor(getResources().getColor(R.color.blue));
			} else if (prefMode == GameMode.DIVISION.ordinal()) {
				btDivision.setBackgroundColor(getResources().getColor(R.color.blue));
			}
			WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
			final int screenWidth = windowManager.getDefaultDisplay().getWidth();
			final int defaultWidth = (int) (screenWidth * 0.8);
//			int defaultHeight = (int) (mApplication.getDimensionDevice().y * 0.9);
			mDialog.setContentView(view);
			mDialog.getWindow().setLayout(defaultWidth, LayoutParams.WRAP_CONTENT);
		}
		if (mDialog != null && !mDialog.isShowing()) {
			mDialog.show();
		}
	}
	
	private void dismissDialog() {
		if (mDialog != null && mDialog.isShowing()) {
			mDialog.dismiss();
		}
	}
	
	@Override
	public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
		Animation animation = null;
		if (!enter) {
			animation = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_out_up);
		} else {
			animation = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in_down);
		}
		return animation;
	}
}
